package com.github.chirspan.xaas.security.component;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.github.chirspan.xaas.security.exception.Auth2Exception;
import lombok.SneakyThrows;

/**
 * @date 2019/2/1
 * <p>
 * OAuth2 异常格式化
 */
public class OAuth2ExceptionSerializer extends StdSerializer<Auth2Exception> {
    public OAuth2ExceptionSerializer() {
        super(Auth2Exception.class);
    }

    @Override
    @SneakyThrows
    public void serialize(Auth2Exception value, JsonGenerator gen, SerializerProvider provider) {
        gen.writeStartObject();
        gen.writeBooleanField("success", false);
        gen.writeObjectField("code", value.getErrorCode());
        gen.writeStringField("msg", value.getMessage());
        gen.writeStringField("data", null);
        gen.writeNumberField("currentTime", System.currentTimeMillis());
        gen.writeEndObject();
    }
}
